package com.zjw.service;


import com.zjw.common.handler.Encrypt;
import com.zjw.common.util.SpringUtils;
import com.zjw.entity.User;
import com.zjw.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author:0xOO
 * @Date: 2018/9/26 0026
 * @Time: 15:23
 */
@Service(value = "userService")
@CacheConfig(cacheNames = {"userCache"})
@Slf4j
public class UserService {

    @Autowired
    UserMapper userMapper;

    @Lazy
    @Autowired
    UserService userService;

    public User Sel(int id){
        User user = userMapper.Sel(id);
        return user;
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public int update(User user) throws Exception {
        int    x = 0;
        try {
            x = userMapper.update(user);
            int y = 1/0;
        } catch (Exception e) {
         //   TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new Exception("0是被除数");
        }
/*        int x = userMapper.update(user);
        int y = 1/0;*/
        return x;
    }

    @Transactional
    public int selectAndUpdate(User user) throws Exception {
        User u = new User();
        u.setId(2);
        u.setPassWord("111111");
        u.setRealName("allen");
        u.setUserName("higher");
     //   add(u);


   //     int x = update(user);

        UserService userService = SpringUtils.getBean("userService");
        userService.update(user);
 //       userService.update(user);


        userMapper.insert(u);

        return 1;
    }


   // @CachePut(cacheNames = { "user" }, key = "#user.id")//写入缓存，key为user.id,一般该注解标注在新增方法上
  //  @Transactional(rollbackFor = )
    public User add(User user){
        try {
            log.info("开始新增数据");
            userMapper.insert1(user,new Encrypt(user.getPassWord()));
          //  int i = 1/0;
        } catch (Exception e) {
            log.error("新增数据异常");
            throw new RuntimeException(e);
        }
        return user;
    }

    public void insert(User user){

    }

    @Cacheable(cacheNames = "user", key = "#id") // 标志读取缓存操作，如果缓存不存在，则调用目标方法，并将结果放入缓存
    public User getOne(int id) {

        System.out.println("缓存不存在，执行方法");
        return userMapper.Sel(id);
    }

    @Cacheable(cacheNames = "userList") // 标志读取缓存操作，如果缓存不存在，则调用目标方法，并将结果放入缓存
    public List<User> getAll() {
        System.out.println("缓存不存在，执行方法");
        return userMapper.SelectAll();
    }

    @CacheEvict(value="userCache", key = "#id",allEntries=false)//方法调用后清空所有缓存
    public int delete(int id) {
        return userMapper.delete(id);
    }

}
